<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('orders', function (Blueprint $table) {
            $table->engine  = 'InnoDB';
            $table->comment('订单');

            $table->increments('id');
            $table->char('trade_no', 32)->comment('sdk交易订单号')->default('');
            $table->string('channel_trade_no', 255)->comment('渠道交易订单号')->default('');
            $table->string('game_trade_no', 64)->comment('游戏方交易订单号')->default('');
            $table->tinyInteger('status')->comment('充值状态 0创建订单 1支付成功 2发货成功 3发货失败')->unsigned()->default(0);
            $table->double('amount', 16, 4)->comment('交易金额')->unsigned()->default(0);
            $table->double('money', 16, 4)->comment('实际扣除金额')->unsigned()->default(0);
            $table->tinyInteger('switch_pay')->comment('是否开启切支付 1是 0否')->unsigned()->default(1);
            $table->tinyInteger('pay_platform')->comment('使用什么支付 appstore,weixin,alipay,...')->unsigned()->default(1);

            $table->string('channel_openid', 128)->comment('渠道用户唯一id')->default('');
            $table->integer('user_id')->comment('充值账号')->unsigned()->default(0);
            $table->integer('package_id')->comment('包id')->unsigned()->default(0);
            $table->integer('game_id')->comment('游戏id')->unsigned()->default(0);
            $table->integer('channel')->comment('渠道标识')->unsigned()->default(0);
            $table->tinyInteger('is_test')->comment('是否为测试订单 1是 0否')->unsigned()->default(0);

            //角色数据
            $table->string('goods_id', 32)->comment('商品id')->default('');
            $table->string('goods_name', 64)->comment('商品名称')->default('');
            $table->integer('server_id')->comment('充值服务器id')->unsigned()->default(0);
            $table->string('server_name', 64)->comment('充值服务器名称')->default('');
            $table->integer('role_id')->comment('充值角色id')->unsigned()->default(0);
            $table->string('role_name', 64)->comment('角色名称')->default('');
            $table->integer('role_level')->comment('充值时角色等级')->unsigned()->default(0);

            $table->integer('create_time')->comment('创建时间')->unsigned()->default(0);
            $table->integer('payok_time')->comment('支付成功时间')->unsigned()->default(0);
            $table->integer('cbok_time')->comment('通知发货成功时间')->unsigned()->default(0);
            $table->tinyInteger('cb_num')->comment('通知发货次数')->unsigned()->default(0);
            $table->integer('last_cb_time')->comment('最后一次通知发货时间')->unsigned()->default(0);
            $table->string('mch_id', 32)->comment('支付商户appid')->default('');
            $table->string('mch_name', 32)->comment('商户名')->default('');
            $table->string('cb_type', 16)->comment('由谁回调支付成功的 server client')->default('');

            $table->integer('lock_version')->comment('乐观锁版本号')->unsigned()->default(1);

            $table->string('ip', 64)->comment('ip')->default('');
            $table->string('net_work', 64)->comment('使用的网络')->default('');
            $table->string('sdk_version', 32)->comment('sdk版本')->default('');

            $table->tinyInteger('device_type')->comment('设备类型 1android 2ios')->unsigned()->default(0);
            $table->string('device_factory', 32)->comment('机型厂商 如 vivo ipone')->default('');
            $table->string('device_os', 32)->comment('设备机型 如 iPhone14')->default('');
            $table->string('device_system', 32)->comment('设备系统版本 如 16.5')->default('');
            $table->integer('device_screen_w')->comment('设备的屏幕分辨率-宽')->unsigned()->default(0);
            $table->integer('device_screen_h')->comment('设备的屏幕分辨率-高')->unsigned()->default(0);

            $table->string('device_num', 32)->comment('自定义唯一id')->default('');
            $table->string('idfa', 64)->comment('ios idfa')->default('');
            $table->string('idfv', 64)->comment('ios idfv')->default('');
            $table->string('imei', 64)->comment('android imei')->default('');
            $table->string('android_id', 64)->comment('google广告id')->default('');
            $table->string('google_adid', 64)->comment('android id')->default('');
            $table->string('oaid', 64)->comment('oaid')->default('');

            $table->unique('trade_no');
            $table->index('create_time');
            $table->index('user_id');
            $table->index('role_id');
            $table->index('game_id');
            $table->index('channel');
            $table->index('status');
        });

        \Illuminate\Support\Facades\DB::statement("ALTER TABLE `orders` add key (channel_trade_no(32));");
        \Illuminate\Support\Facades\DB::statement("ALTER TABLE `orders` add key (channel_openid(32));");
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('orders');
    }
};
